//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-10-03  Jed Young  Creation
//

using chunmapModel

**
** Tile
**
@Js
class CTile : Tile
{
  Grid grid

  new make(Grid grid, Int x, Int y, Int z) : super(x, y, z)
  {
    this.grid = grid
  }

  Str key() { tileXYToQuadKey(x, y, z) }

  Envelope envelope()
  {
    grid.tileEnvelope(this)
  }

  **
  ** see `http://hi.baidu.com/liongg/blog/item/f73ce5dd1514bfe576c6387d.html`
  **
  private static Str tileXYToQuadKey(Int tileX, Int tileY, Int level)
  {
    quadKey := StrBuf()
    for (i := level; i > 0; i--)
    {
      digit := '0'
      Int mask := 1.shiftl(i - 1)
      if ((tileX.and(mask)) != 0)
      {
        ++digit
      }
      if ((tileY.and(mask)) != 0)
      {
        ++digit
        ++digit
      }
      quadKey.add(digit.toChar)
    }
    return quadKey.toStr
  }
}